package model

import (
	"time"

	"bilinadmin/pkg/util"
)

const (
	// EmployeeStatusOK 正常
	EmployeeStatusOK int8 = 0
	// EmployeeStatusInviting 邀请中
	EmployeeStatusInviting int8 = 1
	// EmployeeStatusDepart 离职
	EmployeeStatusDepart int8 = 2

	// EmployeeStatusOKStr 正常
	EmployeeStatusOKStr string = "ok"
	// EmployeeStatusInvitingStr 邀请中
	EmployeeStatusInvitingStr string = "inviting"
	// EmployeeStatusDepartStr 离职
	EmployeeStatusDepartStr string = "depart"
)

// Employee 员工表
type Employee struct {
	Id        int64     `json:"id" sql:"id"`
	BrandId   int64     `json:"brand_id" sql:"brand_id"`
	Name      string    `json:"name" sql:"name"`
	Mobile    string    `json:"mobile" sql:"mobile"`
	Email     string    `json:"email" sql:"email"`
	AccountId string    `json:"account_id" sql:"account_id"`
	EntryDate time.Time `json:"entry_date" sql:"entry_date"`
	Status    int8      `json:"status" sql:"status"`         // 状态：0-正常，1-邀请中，2-离职
	CreatedAt time.Time `json:"created_at" sql:"created_at"` // 创建时间
	UpdatedAt time.Time `json:"updated_at" sql:"updated_at"` // 更新时间
	DeletedAt int64     `json:"delete_at" sql:"deleted_at"`  // 删除时间
}

// NewEmployee 新建员工
func NewEmployee(brandId int64, name, mobile, email, accountId string, status int8, entryDate time.Time) *Employee {
	return &Employee{
		BrandId:   brandId,
		Name:      name,
		Mobile:    mobile,
		Email:     email,
		AccountId: accountId,
		EntryDate: entryDate,
		Status:    status,
		CreatedAt: util.GetNow(),
		UpdatedAt: util.GetNow(),
	}
}

// GetInnerEmpStatus 获取内部员工状态
func GetInnerEmpStatus(st string) int8 {
	switch st {
	case EmployeeStatusOKStr:
		return EmployeeStatusOK
	case EmployeeStatusInvitingStr:
		return EmployeeStatusInviting
	case EmployeeStatusDepartStr:
		return EmployeeStatusDepart
	default:
		return -1
	}
}

// IsValid 是否有效
func (e *Employee) IsValid() bool {
	return e.Status == EmployeeStatusOK && e.DeletedAt == 0
}

// IsInviting 邀请中
func (e *Employee) IsInviting() bool {
	return e.Status == EmployeeStatusInviting && e.DeletedAt == 0
}

// Enable 设置为正常状态
func (e *Employee) Enable(accId string) {
	e.Status = EmployeeStatusOK
	e.AccountId = accId
	e.UpdatedAt = util.GetNow()
}

// Depart 离职
func (e *Employee) Depart() {
	e.Status = EmployeeStatusDepart
	e.UpdatedAt = util.GetNow()
}

// Inviting 邀请
func (e *Employee) Inviting() {
	e.Status = EmployeeStatusInviting
	e.UpdatedAt = util.GetNow()
}

// 删除
func (e *Employee) delete() {
	e.DeletedAt = util.GetCurrentTimestamp()
}

// GetStatus 获取状态
func (e *Employee) GetStatus() string {
	switch e.Status {
	case EmployeeStatusOK:
		return EmployeeStatusOKStr
	case EmployeeStatusInviting:
		return EmployeeStatusInvitingStr
	case EmployeeStatusDepart:
		return EmployeeStatusDepartStr
	default:
		return ""
	}
}

// Update 更新信息
func (e *Employee) Update(name string, entryDate time.Time) {
	if name != "" {
		e.Name = name
	}
	if !entryDate.IsZero() {
		e.EntryDate = entryDate
	}
	e.UpdatedAt = util.GetNow()
}
